using System;
using System.Data;
using Sharepoint.ImportExport.Extractor;
using SharePoint.ImportExport.Core;


namespace cmdSharePointExtractor
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class cmdUI
    {
        static string version = "1.0";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Parameters parameters = new Parameters();
            if (args.Length < 6)
            {
                PrintUsage();
                return;
            }

            if (!parameters.ReadParameters(args))
            {
                PrintUsage();
                return;
            }

            Sharepoint.ImportExport.Extractor.Exporter extractor = new Exporter();
            DataSet ds = new DataSet("Export Parameters");
            DataTable dt = ds.Tables.Add("Parameters");

            dt.Columns.Add("SiteURL");
            dt.Columns.Add("OutputDirectory");
            dt.Columns.Add("SiteName");
            dt.Columns.Add("List");
            dt.Columns.Add("Owner");
            dt.Columns.Add("Contact");
            dt.Columns.Add("IncludeSubWebs", typeof(bool));
            dt.Columns.Add("ManagedPath");
            dt.Columns.Add("SiteDef");
            dt.Columns.Add("Folder");
            dt.Columns.Add("ExportItemsNewerThan", typeof(System.DateTime));
            dt.Columns.Add("ExportItemsOlderThan", typeof(System.DateTime));
            dt.Columns.Add("ExportDesign", typeof(bool));
            dt.Columns.Add("ExportUsers", typeof(bool));
            dt.Columns.Add("ExportVersions", typeof(bool));
            dt.Columns.Add("ExportDoclibsOnly", typeof(bool));
            dt.Columns.Add("ProcessGalleries", typeof(bool));
            dt.Columns.Add("ExportViews", typeof(bool));

            DataRow newRow = dt.NewRow();

            // Set values in the columns:
            newRow["SiteURL"] = parameters.SiteURL;
            newRow["OutputDirectory"] = parameters.OutputDirectory;
            newRow["SiteName"] = parameters.NewName;
            newRow["List"] = parameters.List;
            newRow["Owner"] = parameters.Owner;
            newRow["Contact"] = parameters.Contact;
            newRow["IncludeSubWebs"] = parameters.IncludeSubWebs;
            newRow["ManagedPath"] = parameters.Root;
            newRow["Folder"] = parameters.Folder;
            newRow["ExportItemsNewerThan"] = parameters.ExportItemsNewerThan;
            newRow["ExportDesign"] = parameters.ExtractDesign;
            newRow["ExportUsers"] = parameters.ExportUserInfo;
            newRow["ExportItemsOlderThan"] = parameters.ExportItemsOlderThan;
            newRow["ExportVersions"] = parameters.ExportVersions;
            newRow["ExportDoclibsOnly"] = false;
            newRow["ProcessGalleries"] = false;
            newRow["ExportViews"] = false;

            // Add the row to the rows collection.
            dt.Rows.Add(newRow);
            Logger.Init("export" + DateTime.Now.ToString("MMddHHmmss") + ".log", ErrorLevel.Information);
            Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);

            try
            {

                extractor.Process(ds, parameters.Compress);
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception occurred :" + Ex.Message);
            }

            Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
            Logger.Close();
        }

        private static void IncomingEventLogData(object sender, SPEventArgs e)
        {
            string logArgs = DateTime.Now.ToLongTimeString() + ":  " + e.Message;
            Console.WriteLine(logArgs);
        }


        static void PrintUsage()
        {
            Console.WriteLine("SharePoint data extractor Version {0}", version);
            Console.WriteLine();
            Console.WriteLine("---");
            Console.WriteLine("Usage:");
            Console.WriteLine("-url URL to the site/area to be extracted");
            Console.WriteLine("-newname site name of the new site");
            Console.WriteLine("-out x:\\outputfolder");
            Console.WriteLine();
            Console.WriteLine("Optional:");
            Console.WriteLine("-root [Sites|Topics]");
            Console.WriteLine("-list Name of list/library to be extracted");
            Console.WriteLine("-folder Name of folder to be extracted");
            Console.WriteLine("-includesubwebs true/false");
            Console.WriteLine("-exportuserinfo true/false");
            Console.WriteLine("-exportdesign true/false");
            Console.WriteLine("-exportversioninfo true/false");
            Console.WriteLine("-owner DOMAIN\\User");
            Console.WriteLine("-contact user@domain.com");
            Console.WriteLine("-exportitemsnewerthan DateTime threshold, if specified then items newer than the threshold will be migrated");
            Console.WriteLine("-exportitemsolderthan DateTime threshold, if specified then items older than the threshold will be migrated");
            Console.WriteLine("-compress true/false");
            //			Console.WriteLine("-reportLevel Information|Warning|Error");
            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}